<template>
  <Select placeholder="请选择" @on-change="onChange" :label-in-value="labelInValue" v-model='orgId'>
    <Option v-for="item in twoLevel" :value="orgCode ? item.orgCode:item.orgId" :key="item.orgId">{{
        item.orgName
      }}
    </Option>
  </Select>
</template>

<script>
import {Api} from "@/api";

export default {
  name: "twoLevelOrg",
  props: {
    orgCode: {
      type: Boolean,
      default: false,
    },
    labelInValue: {
      type: Boolean,
      default: false,
    },
    value: {
      type: String,
    }
  },
  data() {
    return {
      orgId: null,
      twoLevel: [],
    }
  },
  created() {
    this.$http.get(Api.quqeryTwoOrgInfo).then(res => {
      this.twoLevel = res.data.data
    })
  },
  methods: {
    onChange(data) {
      this.$emit("on-change", data)
      if(this.orgCode) {
        this.twoLevel.forEach(item => {
          if (data == item.orgCode) {
            this.$emit("on-change-name", item.orgName);
          }
        })
      }
    }
  },
  watch: {
    value: {
      immediate: true,
      handler(n) {
        this.orgId = n;
      },
    },
    orgId(n, o) {
      this.$emit("input", n)
    }
  },
}
</script>

<style scoped>

</style>
